package com.py.credit.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

import com.py.credit.clean.code.XydmCheck;
import com.py.credit.constant.ConstantData;

public class DateCheckUtils {

	@SuppressWarnings("deprecation")
	public static List<Map<String, Object>> sgsDateCheck(List<Map<String, Object>> sgsMap,
			List<Map<String, Object>> dateList, String table, String value) throws ParseException {
		String isWorkDay = "";
		List<Map<String, Object>> list = new ArrayList<>();
		List<Date> dateData = new ArrayList<>();
		if (sgsMap != null) {
			for (int i = 0; i < sgsMap.size(); i++) {
				int count = 0;
				// 获取上报时间
				Date date = (Date) sgsMap.get(i).get("creditTime");
				// 获取公示决定日期
				Date dateYxq = (Date) sgsMap.get(i).get(value);
				// 获取记录ID
				Integer recordId = (Integer) sgsMap.get(i).get("record_id");

				if (dateYxq == null) {
					isWorkDay = ConstantData.NO_WORK_DAY;
					Map<String, Object> map = new HashMap<>();
					map.put("recordId", recordId);
					map.put("status", isWorkDay);
					map.put("table", table);
					list.add(map);
					continue;
				}
				if (date.getTime() < dateYxq.getTime()) {
					isWorkDay = ConstantData.NO_WORK_DAY;
					Map<String, Object> map = new HashMap<>();
					map.put("recordId", recordId);
					map.put("status", isWorkDay);
					map.put("table", table);
					list.add(map);
					continue;
				}
				Calendar cal = Calendar.getInstance();
				// 将时间转换为long型用于求下一天时间
				long dateCount = dateYxq.getTime();
				if (!CollectionUtils.isEmpty(dateList)) {
					while (count < 7) {
						dateCount = dateCount + ConstantData.DAY;
						// 将long型转为date型
						Date dateSw = new Date(dateCount);
						cal.setTime(dateSw);
						// 遍历查出的工作日时间
						for (int j = 0; j < dateList.size(); j++) {
							String compareDate = (String) dateList.get(j).get("DATE");
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
							sdf.parse(compareDate);
							String checkWork = (String) dateList.get(j).get("IS_WORK_DAY");
							if (dateSw.getYear() == sdf.parse(compareDate).getYear()
									&& dateSw.getDate() == sdf.parse(compareDate).getDate()
									&& dateSw.getMonth() == sdf.parse(compareDate).getMonth()
									&& checkWork.equals("0")) {
								break;
							}
							if ((dateSw.getYear() == sdf.parse(compareDate).getYear()
									&& dateSw.getDate() == sdf.parse(compareDate).getDate()
									&& dateSw.getMonth() == sdf.parse(compareDate).getMonth()
									&& checkWork.equals("1"))) {
								dateData.add(dateSw);
								count++;
								break;
							}
							// 判断改时间是不是工作日时间，如果是则在遍历完之后插入list中
							if (!(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
									|| cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
								if (j == dateList.size() - 1) {
									dateData.add(dateSw);
									count++;
								}
							}
						}
					}
					//若后两天是周末，也算入
					for (int j = 0; j < 2; j++) {
						dateCount = dateCount + ConstantData.DAY;
						// 将long型转为date型
						Date dateSw = new Date(dateCount);
						cal.setTime(dateSw);
						if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
								|| cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
							dateData.add(dateSw);
						} else {
							break;
						}
					}
				} else {
					while (count < 7) {
						dateCount = dateCount + ConstantData.DAY;
						// 将long型转为date型
						Date dateSw = new Date(dateCount);
						cal.setTime(dateSw);
						if (!(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
								|| cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
							dateData.add(dateSw);
							count++;
						}
					}
					//若后两天是周末，也算入
					for (int j = 0; j < 2; j++) {
						dateCount = dateCount + ConstantData.DAY;
						// 将long型转为date型
						Date dateSw = new Date(dateCount);
						cal.setTime(dateSw);
						if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
								|| cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
							dateData.add(dateSw);
						} else {
							break;
						}
					}
				}
				Date dateCal = dateData.get(dateData.size() - 1);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				// 将工作日七天后的时间与上交时间作比
				if (sdf.parse(sdf.format(date)).getTime() <= sdf.parse(sdf.format(dateCal)).getTime()) {
					isWorkDay = ConstantData.WORK_DAY;
					Map<String, Object> map = new HashMap<>();
					map.put("recordId", recordId);
					map.put("status", isWorkDay);
					map.put("table", table);
					list.add(map);
				} else {
					isWorkDay = ConstantData.NO_WORK_DAY;
					Map<String, Object> map = new HashMap<>();
					map.put("recordId", recordId);
					map.put("status", isWorkDay);
					map.put("table", table);
					list.add(map);
				}

			}
		}
		return list;
	}

}
